#include "HealthPack.h"

#include "InputHandler.h"
#include "Utility.h"
#include "GameGlobals.h"
#include "Resources.h"
#include "BaseMoveable.h"

LINKCLASSTONAME("ent_healthpack", HealthPack);

HealthPack::HealthPack(sf::RenderWindow *App) : BaseMoveable(App)
{
	DrawOrder = 2;

	sprHealth.SetTexture(*Resources::Instance()->RequestImage("healthpack"));
	sprHealth.SetOrigin(sprHealth.GetSize().x / 2, (sprHealth.GetSize().y / 2) + 3);

	sf::Vector2f Points[4];
	float height, width;
	height = 50;
	width = 50;
	Points[0] = sf::Vector2f(-width / 2,height / 2);
	Points[1] = sf::Vector2f(width / 2, height / 2);
	Points[2] = sf::Vector2f(width / 2, -height / 2);
	Points[3] = sf::Vector2f(-width / 2, -height / 2);
	CreatePhysics();
	GetPhysicsObject()->PhysicsInitPolygon(this, Points, 4);
	GetPhysicsObject()->SetMass(1);
}

HealthPack::~HealthPack(void)
{
}

void HealthPack::OnCollide(BaseMoveable *ent1, BaseMoveable *ent2, CollisionData CollideInfo)
{
	if (ent2->GetClass() == std::string("ent_player"))
	{
		float dHealth = 25;
		if (ent2->GetHealth() + 25 > ent2->GetMaxHealth())
		{
			 dHealth = ent2->GetMaxHealth() - ent2->GetHealth();
		}
		ent2->AddHealth(dHealth);
		Delete();
	}
}

bool HealthPack::ShouldCollide(BaseMoveable *other)
{
	if (other->GetClass() == std::string("ent_player"))
	{
		return true;
	}
	return false;
}

void HealthPack::Think()
{

}

void HealthPack::Draw()
{
	float scale = (std::sin(GameGlobals::Instance()->CurTime * 4.f) * 0.1) + 1.f;
	sprHealth.SetScale(scale,scale);
	sprHealth.SetRotation(GetAngle());
	sprHealth.SetPosition(GetSFMLPos());
	pRender->Draw(sprHealth);
	GetPhysicsObject()->Draw();
}

void HealthPack::OnDelete()
{

}